<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>账号管理系统</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        .search-box {
            margin-bottom: 20px;
        }
        .search-box input {
            padding: 8px;
            width: 300px;
        }
        .search-box button {
            padding: 8px 15px;
            margin-left: 10px;
        }
    </style>
</head>
<body>
    <h1>账号管理系统</h1>

    <div class="search-box">
        <input type="text" id="searchInput" placeholder="输入账号进行搜索">
        <button onclick="searchAccounts()">搜索</button>
        <button onclick="loadAccounts()">显示全部</button>
    </div>

    <table id="accountsTable">
        <thead>
            <tr>
                <th>ID</th>
                <th>账号</th>
                <th>支付密码</th>
                <th>状态</th>
                <th>T账号</th>
                <th>创建时间</th>
            </tr>
        </thead>
        <tbody id="accountsTableBody">
            <!-- 数据将通过JavaScript动态加载 -->
        </tbody>
    </table>

    <script>
        // 页面加载时获取所有账号
        window.onload = function() {
            loadAccounts();
        };

        // 获取所有账号
        function loadAccounts() {
            fetch('/api/account/all')
                .then(response => response.json())
                .then(data => {
                    renderAccounts(data);
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('获取数据失败');
                });
        }

        // 搜索账号
        function searchAccounts() {
            const account = document.getElementById('searchInput').value.trim();
            let url = '/api/account/query';

            if (account != null) {
                url += '?account=' + encodeURIComponent(account);
            }

            fetch(url)
                .then(response => response.json())
                .then(data => {
                    renderAccounts(data);
                })
                .catch(error => {
                    console.error('Error:', error);
                    // 修复：使用alert替代未定义的showError函数
                    alert('搜索失败: ' + error.message);
                });
        }




        // 渲染账号表格
        function renderAccounts(accounts) {
            const tbody = document.getElementById('accountsTableBody');
            tbody.innerHTML = '';

            accounts.forEach(account => {
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>${account.id}</td>
                    <td>${account.account}</td>
                    <td>${account.payPassword}</td>
                    <td>${account.isUse === 1 ? '启用' : '禁用'}</td>
                    <td>${account.taccount}</td>
                    <td>${new Date(account.dateTime).toLocaleString()}</td>
                `;
                tbody.appendChild(row);
            });
        }

        // 回车搜索
        document.getElementById('searchInput').addEventListener('keypress', function(event) {
            if (event.key === 'Enter') {
                searchAccounts();
            }
        });
    </script>
</body>
</html>
